/*
Program: DC-SAR, File stringfunc.c
Copyright (C) 2009 Davy De Coninck

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stringfunc.h"
#include <string.h>
#include <stdlib.h>

char *substr(const char *pstr, int start, int numchars)
{
	char *pnew = malloc(numchars+1);
	strncpy(pnew, pstr + start, numchars);
	pnew[numchars] = '\0';
	return pnew;
}

/*
t1 = string to replace
t2 = with what it will be replaced
t6 = input string
*/

char *str_replace(char * t1, char * t2, char * t6)
{
	char*t4;
	char*t5=malloc(0);


	while(strstr(t6,t1)){
		t4=strstr(t6,t1);
		strncpy(t5+strlen(t5),t6,t4-t6);
		strcat(t5,t2);
		t4+=strlen(t1);
		t6=t4;
	}
	return strcat(t5,t4);
}

int str_split(char *string, char *c_fields[], int nc_fields, char *sep)
{
	register char *p = string;
	register char c;			/* latest character */
	register char sepc = sep[0];
	register char sepc2;
	register int fn;
	register char **fp = c_fields;
	register char *sepp;
	register int trimtrail;

	/* white space */
	if (sepc == '\0') {
		while ((c = *p++) == ' ' || c == '\t')
			continue;
		p--;
		trimtrail = 1;
		sep = " \t";	/* note, code below knows this is 2 long */
		sepc = ' ';
	} else
		trimtrail = 0;
		sepc2 = sep[1];		/* now we can safely pick this up */

		/* catch empties */
		if (*p == '\0')
			return(0);

		/* single separator */
		if (sepc2 == '\0') {
			fn = nc_fields;
			for (;;) {
				*fp++ = p;
				fn--;
				if (fn == 0)
					break;
				while ((c = *p++) != sepc)
					if (c == '\0')
						return(nc_fields - fn);
				*(p-1) = '\0';
			}
			/* we have overflowed the c_fields vector -- just count them */
			fn = nc_fields;
			for (;;) {
				while ((c = *p++) != sepc)
					if (c == '\0')
						return(fn);
				fn++;
			}
			/* not reached */
		}

		/* two separators */
		if (sep[2] == '\0') {
			fn = nc_fields;
			for (;;) {
				*fp++ = p;
				fn--;
				while ((c = *p++) != sepc && c != sepc2)
					if (c == '\0') {
					if (trimtrail && **(fp-1) == '\0')
						fn++;
					return(nc_fields - fn);
					}
					if (fn == 0)
						break;
					*(p-1) = '\0';
					while ((c = *p++) == sepc || c == sepc2)
						continue;
					p--;
			}
			/* we have overflowed the c_fields vector -- just count them */
			fn = nc_fields;
			while (c != '\0') {
				while ((c = *p++) == sepc || c == sepc2)
					continue;
				p--;
				fn++;
				while ((c = *p++) != '\0' && c != sepc && c != sepc2)
					continue;
			}
			/* might have to trim trailing white space */
			if (trimtrail) {
				p--;
				while ((c = *--p) == sepc || c == sepc2)
					continue;
				p++;
				if (*p != '\0') {
					if (fn == nc_fields+1)
						*p = '\0';
					fn--;
				}
			}
			return(fn);
		}

		/* n separators */
		fn = 0;
		for (;;) {
			if (fn < nc_fields)
				*fp++ = p;
			fn++;
			for (;;) {
				c = *p++;
				if (c == '\0')
					return(fn);
				sepp = sep;
				while ((sepc = *sepp++) != '\0' && sepc != c)
					continue;
				if (sepc != '\0')	/* it was a separator */
					break;
			}
			if (fn < nc_fields)
				*(p-1) = '\0';
			for (;;) {
				c = *p++;
				sepp = sep;
				while ((sepc = *sepp++) != '\0' && sepc != c)
					continue;
				if (sepc == '\0')	/* it wasn't a separator */
					break;
			}
			p--;
		}

		/* not reached */
}
